{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35deff53",
   "metadata": {},
   "source": [
    "## ndarray数组的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "549507f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8028dd7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0., 20., 40., 60., 80.], dtype=float32)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.arange(0,100,20,dtype=np.float32)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a4e36d1",
   "metadata": {},
   "source": [
    "1. ndarray.ndim函数获取数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e9d109cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "82286c13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 0, 7],\n",
       "       [2, 0, 5]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2 = np.random.randint(0,9,size=(2,3))\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6540cfca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ee93cf4",
   "metadata": {},
   "source": [
    "2. ndarray.shape函数获取数组的形状\n",
    "\n",
    "函数返回数组维度的元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7be88016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "34fb7cdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "425e2338",
   "metadata": {},
   "source": [
    "3. ndarray.reshape函数将数组变形\n",
    "\n",
    "变形后的行列数需要同变形前的总数保持一致。<br>\n",
    "变形过程不影响原来的数组<br>\n",
    "参数是一个元组，有几个值，就是变形后的几维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f17f4c79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 0],\n",
       "       [7, 2],\n",
       "       [0, 5]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2.reshape(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fbc3fd61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[4, 0, 6],\n",
       "        [6, 0, 0],\n",
       "        [1, 0, 7]],\n",
       "\n",
       "       [[5, 7, 6],\n",
       "        [4, 3, 6],\n",
       "        [4, 7, 0]]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3 = np.random.randint(0,9,size=(2,3,3))\n",
    "d3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8e9ae5c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 0, 6, 6, 0, 0],\n",
       "       [1, 0, 7, 5, 7, 6],\n",
       "       [4, 3, 6, 4, 7, 0]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3.reshape(3,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dc520994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 0 6 6 0 0 1 0 7 5 7 6 4 3 6 4 7 0]\n",
      "[4 0 6 6 0 0 1 0 7 5 7 6 4 3 6 4 7 0]\n"
     ]
    }
   ],
   "source": [
    "#变成一维数组时，使用元组参数，(n,)\n",
    "print(d3.reshape(18))\n",
    "print(d3.reshape((18,)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ab62e3f",
   "metadata": {},
   "source": [
    "3. ndarray.size获取数组的元素个数<br>\n",
    "ndarry.itemsize获取每个元素的占用内存空间的大小，单位是字节,每个字节为8位。int32返回itemsize为4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bea08ed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e08dd1b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3.itemsize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "67cc6215",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9bab5e6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "d4 = np.arange(10**8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "11cefaf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.450580596923828"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d4.nbytes/1024**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e9053d71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d4.dtype"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
